package com.xyulu.job;

import com.xyulu.entity.common.RedisConstant;
import com.xyulu.utils.FileUtil;
import com.xyulu.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

/**
 * @Author Xyulu
 * @Date 2021/4/24 21:42
 * @Version 1.0
 * @Description 定时清理垃圾文件
 */
//@Component
public class FileCleanTask {

    @Value("${file.upload.img}")
    String uploadImgPath;

    /**
     * @Description 定时清理垃圾图片。设置每天执行一次
     * @return void
     **/
    @Scheduled(cron = "0 0 0 1/1 * ? ")
    private void cleanImg(){
        Set<String> sdiff = RedisUtil.sdiff(RedisConstant.COVER_IMG_TEMP, RedisConstant.COVER_IMG_UPLOAD);
        sdiff.forEach(x->{
            FileUtil.deleteFile(uploadImgPath + x);
            RedisUtil.srem(RedisConstant.COVER_IMG_TEMP,x);
        });
    }

}
